import unittest
import requests
import random

'''
1. setUp, tearDown, setUpClass, tearDownClass不一定必须成对, 在具体的场景选择使用即可
2. 测试用例的执行顺序是按照字母表来执行
3. 总结: 操作步骤相同, 不同的是: 传入参数, 验证点=>数据驱动
'''


class TestAddEvent(unittest.TestCase):
    url = 'http://127.0.0.1:8000/api/add_event/'

    def test_add_event_sucess(self):
        self.eid1 = random.randint(20, 60)
        data = {'eid': self.eid1, 'name': 'Test1902-1122', 'limit': 20, 'address': 'F19',
                'start_time': '2019-09-11 12:00:00'}
        r = requests.post(url=self.url, data=data)
        resp = r.json()
        self.assertEqual(resp['status'], 200)
        self.assertEqual(resp['message'], 'add event success')

    def test_param_error(self):
        data = {'eid': '', 'name': 'Test1902-1122', 'limit': 20, 'address': 'F19', 'start_time': '2019-09-11 12:00:00'}
        r = requests.post(url=self.url, data=data)
        resp = r.json()
        self.assertEqual(resp['status'], 10021)
        self.assertEqual(resp['message'], 'parameter error')

    def test_id_exist(self):
        data = {'eid': self.eid1, 'name': 'Test1902-1122', 'limit': 20, 'address': 'F19',
                'start_time': '2019-09-11 12:00:00'}
        r = requests.post(url=self.url, data=data)
        resp = r.json()
        self.assertEqual(resp['status'], 10022)
        self.assertEqual(resp['message'], 'event id already exists')

    def test_name_exist(self):
        self.eid2 = random.randint(60, 100)
        data = {'eid': self.eid2, 'name': 'Test1902-1122', 'limit': 20, 'address': 'F19',
                'start_time': '2019-09-11 12:00:00'}
        r = requests.post(url=self.url, data=data)
        resp = r.json()
        self.assertEqual(resp['status'], 10023)
        self.assertEqual(resp['message'], 'event name already exists')

    def test_time_error(self):
        data = {'eid': self.eid2, 'name': 'Test1902-1122-1', 'limit': 20, 'address': 'F19',
                'start_time': '2019-11 12:00:00'}
        r = requests.post(url=self.url, data=data)
        resp = r.json()
        self.assertEqual(resp['status'], 10024)
        self.assertEqual(resp['message'], 'start_time format error. It must be in YYYY-MM-DD HH:MM:SS format')

    def tearDownClass(cls) -> None:
        # 如果删除发布会接口可用, 可以tearDownClass中删除self.eid1的发布数据
        pass


if __name__ == '__main__':
    unittest.main()
